package com.srit.system.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.srit.core.sql.OrderBy;
import com.srit.forecast.constant.Constant;
import com.srit.system.bean.Code;
import com.srit.system.dao.CodeDao;

/**
 * @author guan
 * 
 */
@Controller
public class CodeController {
	@Autowired
	private CodeDao codeDao;

	@Autowired(required = true)
	private ServletContext context;

	@RequestMapping("/code-form")
	public String form(@RequestParam int codeTypeId, @ModelAttribute Code code) {
		return "system/code";
	}

	@RequestMapping("/code-edit")
	public String edit(@RequestParam int id, @ModelAttribute Code code) {
		BeanUtils.copyProperties(codeDao.getById(id), code);
		return "system/code";
	}

	@RequestMapping(value = "/code-saveOrUpdate", method = RequestMethod.POST)
	public String saveOrUpdateWeatherCode(Code code) {
		if (code.getUploadItem().getSize() != 0) {
			copyFile(code);
		}
		if (code.getId() != null && code.getId() != 0) {
			codeDao.update(code);
		} else {
			codeDao.save(code);
		}
		// foward到查询页面
		return "redirect:/code-form?codeTypeId=" + code.getCodeTypeId();
	}

	private void copyFile(Code code) {
		try {
			BufferedInputStream bio = new BufferedInputStream(code
					.getUploadItem().getFileItem().getInputStream());
			String uploadPath = context.getRealPath(Constant.UPLOAD_PATH);
			String fileName = new Date().getTime()
					+ code.getUploadItem().getOriginalFilename();
			BufferedOutputStream bos = new BufferedOutputStream(
					new FileOutputStream(new File(uploadPath + "/" + fileName)));
			code.setImage(fileName);
			byte[] buffer = new byte[1024];
			int readByte = 0;
			while ((readByte = bio.read(buffer)) != -1) {
				bos.write(buffer, 0, readByte);
			}
			bio.close();
			bos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/code-saveList", method = RequestMethod.POST)
	public String saveCodeList(CodeListVo codeListVo,
			@RequestParam int codeTypeId) {
		// TODO 这里有问题
		for (Iterator<Code> it = codeListVo.getCodeList().iterator(); it
				.hasNext();) {
			Code code = it.next();
			copyFile(code);
			codeDao.save(code);
		}
		return "redirect:/codeType-edit?id=" + codeTypeId;
	}

	@RequestMapping(value = "/codeByTypId.json")
	public @ResponseBody
	List<Code> jsonListByTypeId(@RequestParam int codeTypeId) {
		OrderBy orderBy = new OrderBy();
		return codeDao.listByCodeTypeId(codeTypeId, orderBy);
	}

	@RequestMapping(value = "/codeById.json")
	public @ResponseBody
	Code jsonCodeById(@RequestParam int id) {
		return codeDao.getById(id);
	}

}

class CodeListVo {
	private List<Code> codeList;

	public List<Code> getCodeList() {
		return codeList;
	}

	public void setCodeList(List<Code> codeList) {
		this.codeList = codeList;
	}

}
